package com.focus.xyz.union.pages.backend;

import com.focus.xyz.core.common.generator.DriverFactory;
import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BasePage;
import com.focus.xyz.core.utils.SikuliUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.data.InsDataBean;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.InsPlanBean;
import org.apache.commons.lang.time.DateUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.text.SimpleDateFormat;

/**
 * Created by chenmeng on 2016/5/26.
 */
public class BackNewOrderPage extends BasePage {
    private Action action;

    @FindBy(id = "loginName")
    private WebElement loginName;
    @FindBy(id = "check")
    private WebElement checkBtn;
    @FindBy(id = "prodName")
    private WebElement prodName;
    @FindBy(xpath = "//input[@id='prodName']/following-sibling::ul/li")
    private WebElement prodNameDiv;
    @FindBy(id = "destSearch")
    private WebElement destSearch;
    @FindBy(xpath = "//div[@id='div_dest']/div/table/tbody/tr/td/input")
    private WebElement destCheck;
    @FindBy(xpath = "//*[@id=\"dest\"]/td/input[2]")
    private WebElement destBtn;
    @FindBy(id = "dev_period")
    private WebElement periodSelect;
    @FindBy(id = "startTime")
    private WebElement startTime;
    @FindBy(id = "endTime")
    private WebElement endTime;
    @FindBy(id = "btnSubmit")
    private WebElement btnSubmit;
    @FindBy(xpath = "//div[@id='uploadControl']/a[1]")
    private WebElement uploadLink;




    public BackNewOrderPage(WebDriver driver, Action action) {
        super(driver, action);
        this.action = action;
        PageFactory.initElements(driver,this);
    }

    public void toubao(String memName,InsDataBean bean) throws Exception{
        action.inputText(loginName,memName);
        action.click(checkBtn);
        action.inputText(this.prodName,bean.getProdName());
        action.click(prodNameDiv);
        InsPlanBean ipBean = InsDataService.getInsPlanById(bean.getPlanId());
        action.inputText(destSearch,ipBean.getDestination());
        action.click(destBtn);
        action.click(destCheck);
        action.select(periodSelect,ipBean.getDev_period());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dbDate = DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getSysDate();
        int i = DateUtils.truncatedCompareTo(sdf.parse(dbDate),sdf.parse(ipBean.getDev_startTime()),9);
        if(i >= 0){
            ipBean.setDev_startTime(DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getNextDate(1));
        }
        i = DateUtils.truncatedCompareTo(sdf.parse(dbDate),sdf.parse(ipBean.getDev_endTime()),9);
        if(i >= 0){
            ipBean.setDev_endTime(DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getNextDate(1));
        }

        action.removeAttribute(startTime,"readonly");
        action.removeAttribute(endTime,"readonly");
        action.inputText(startTime,ipBean.getDev_startTime());
        action.inputText(endTime,ipBean.getDev_endTime());
        action.esc();
        /*
        * 上传被保人清单
        * */
        SikuliUtil.flashUpload("flashupload.png","open.png","filepath.png","E:\\cimon\\union\\data\\huatai_2.xls");
        action.pause(1000l);
        action.click(uploadLink);
        action.pause(1000l);
        action.click(btnSubmit);
        if(action.isAlertPresent()){
            action.acceptAlert();
        }
    }
}
